<?php

use Biz\model\store\StoreCaseModel;
use Biz\service\Store\StoreHomeService;
use Doraemon\pockets\constInterface\statSign\AutoModule;
use Doraemon\service\statSign\Basic as basicStatSign;


/**
 * Description: 获取案例列表
 * Created by IntelliJ IDEA.
 * User: 玉坤
 * Date: 2021/7/29
 * Time: 13:58
 */
class CaseListAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_NOTHING;
    public const PAGE_SIZE = 20;

    public function main()
    {
        $store_id = $this->getRequest()->getPost('store_id', 0);
        $per_page = $this->getRequest()->getPost('per_page', self::PAGE_SIZE);
        $page = $this->getRequest()->getPost('page', 1);
        if (!$store_id) {
            throw new Exception('非法传参');
        }
        $data = $this->_getCaseInfo($store_id, $page, $per_page);
        $this->setSuccess($data);
    }


    /**
     * 获取案例信息
     * @param $store_id
     * @param $page
     * @param $per_page
     * @return array
     */
    private function _getCaseInfo($store_id, $page, $per_page): array
    {
        $data = [
            'total' => 0,
            'is_over' => 1,
            'list' => [],
        ];
        $field = ['store_id', 'case_id', 'house_area', 'house_price', 'id'];

        $data['total'] = StoreCaseModel::getPassCount($store_id) ?: 0;
        if (!$data['total']) {
            return $data;
        }
        $where_ext['check_status'] = StoreCaseModel::PASS;
        $start = ($page - 1) * $per_page;
        $where_ext["LIMIT"] = [$start, $per_page];
        $list = StoreHomeService::getCaseList($store_id, $field, $where_ext);
        if ($list) {
            foreach ($list as &$item) {
                $obj_type = \Doraemon\tools\IdParse::getType($item['case_id']);
                $item['statSign'] = basicStatSign::genStatSign(AutoModule::MODULE_STORE_SEC_PAGE_CASE_LIST, [basicStatSign::EXTEND_KEY_OBJ_ID=> $item['case_id'],basicStatSign::EXTEND_KEY_OBJ_TYPE=>$obj_type??null],
                    ['store_id' => $store_id, 'act_from' => 'store_sec_page']);
            }
        }
       $list= \Doraemon\tools\StatSign::versionCompatible($list, \Doraemon\tools\StatSign::BRAND_UP_GRADE_VERSION_521, \Doraemon\tools\BlockType::TYPE_STORE_CASE, 'sub_case');

        $data['list'] = $list ?: [];
        $data['is_over'] = ($data['total'] <= count($list)) ? 1 : 0;
        return $data;
    }
}